package com.meizi.daily.rest;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.meizi.core.tool.api.PageResult;
import com.meizi.daily.entity.Category;
import com.meizi.core.tool.api.PageParam;
import com.meizi.core.tool.api.R;
import com.meizi.daily.biz.CategoryBiz;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryBiz baseBiz;

    @PostMapping
    public R addCategory(@RequestBody Category category) {
        baseBiz.addCategory(category);
        return new R().ofSuccess();
    }
    @DeleteMapping(value = "/{id}")
    public R remove(@PathVariable int id){
        baseBiz.deleteById(id);
        return  new R().ofSuccess();
    }
    @PutMapping(value = "/{id}")
    public R update(@RequestBody Category entity){
        baseBiz.updateSelectiveById(entity);
        return  new R().ofSuccess();
    }
    @GetMapping("/page")
    public R page(Category category, PageParam pageInfo) {
        PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
        List<Category> list = baseBiz.select(category);
        PageInfo<Category> of = PageInfo.of(list);
        return new R().ofSuccess(new PageResult(list, of.getTotal()));
    }

    @GetMapping("/hierarchy/page")
    public R hierarchy(PageParam pageInfo) {
        return baseBiz.hierarchy(pageInfo);
    }
}
